/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package alpes.aerolinea;

import alpes.aerolinea.entities.TipoUsuario;
import alpes.aerolinea.entities.Usuario;
import alpes.aerolinea.services.ILoginService;
import alpes.aerolinea.services.LogInServiceMock;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;

/**
 *
 * @author Daniel
 */
public class logInBean {
    // -----------------------------------------------
    // Constantes
    // -----------------------------------------------

    /**
     * Constante que modela el valor de error en la operación de ingreso al sistema.
     */
    private final static String ERROR = "ERROR";
    private String nombre;
    private String username;
    private String pwd;
    @EJB
    private ILoginService loginService;

    /** Creates a new instance of logInBean */
    public logInBean() {
        loginService = new LogInServiceMock();
    }

    public String logIn() {
        try {
            System.out.println(loginService);
            Usuario user = loginService.login(username, pwd);
            System.out.println("LogIn!! " + user.getTipoUsuario());
            if (user.getTipoUsuario() == TipoUsuario.ADMINISTRADOR) {
                return "Administrador";
            } else {
                return "Cliente";
            }
        } catch (Exception ex) {
            FacesContext context = FacesContext.getCurrentInstance();
            String msg = ex.getMessage();
            FacesMessage fm = new FacesMessage(msg);
            fm.setSeverity(FacesMessage.SEVERITY_ERROR);
            context.addMessage(null, fm);
            Logger.getLogger(logInBean.class.getName()).log(Level.SEVERE, null, ex);
            return ERROR;
        }
    }
    public String entrarClienteComun()
    {
        return "ClienteComun";
    }
        public String entrarKids()
    {
        return "Kids";
    }
    public String getNombre() {
        return nombre;
    }

    public void setNombre(String nombre) {
        this.nombre = nombre;
    }

    public String getPwd() {
        return pwd;
    }

    public void setPwd(String pwd) {
        this.pwd = pwd;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }
}
